package com.vcredit.creditcat.util;

import java.lang.reflect.Field;

import org.apache.commons.lang.StringUtils;

public class RCUtils {
	@SuppressWarnings("unchecked")
	public static <T> String convertToRCparam(T target) throws IllegalArgumentException, IllegalAccessException {
		StringBuilder sBuilder = new StringBuilder();

		sBuilder.append("<?xml version = \"1.0\" encoding = \"utf-8\" ?>");
		sBuilder.append("<ArrayOfVBXML>");

		Class<T> targetClass = (Class<T>) target.getClass();

		Field[] fs = targetClass.getDeclaredFields();
		for (int i = 0; i < fs.length; i++) {
			Field f = fs[i];
			f.setAccessible(true);
			sBuilder.append("<VBXML>");
			sBuilder.append("<VB_COL>");
			sBuilder.append(f.getName());
			sBuilder.append("</VB_COL>");
			sBuilder.append("<VB_VALUE>");
			sBuilder.append(f.get(target));
			sBuilder.append("</VB_VALUE>");
			sBuilder.append("</VBXML>");
		}

		sBuilder.append("</ArrayOfVBXML>");

		return sBuilder.toString();
	}

	public static String convertDecimalParam(String param) {
		return StringUtils.isNotEmpty(param) ? param : "0";
	}
}
